package com.yhg.qxmall.socket.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期操作工具类
 * @author 凌月青缃
 */
public class DateUtil {
//    SimpleDateFormat sdf = new SimpleDateFormat("YYYY/MM/dd HH:mm:ss.SSS");
    private static final String dateFormat = "yyyy-MM-dd";
    private static final String DetailFormat = "HH:mm";
    private static final String AllFormat = "yyyy-MM-dd HH:mm";

    /**
     * 精细化日期
     * @param date
     * @return
     */
    public static String AllDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(AllFormat);
        return sdf.format(date);
    }

    /**
     * 精细化日期
     * @param date
     * @return
     */
    public static String DetailDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(DetailFormat);
        return sdf.format(date);
    }

    /**
     * 格式化日期
     *
     * @param date
     * @return
     */
    public static String formatDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        return sdf.format(date);

    }

    /**
     * 在日期date上增加amount天 。
     *
     * @param date   处理的日期，非null
     * @param amount 要加的天数，可能为负数
     */
    public static Date addDays(Date date, int amount) {
        Calendar now =Calendar.getInstance();
        now.setTime(date);
        now.set(Calendar.DATE,now.get(Calendar.DATE)+amount);
        return now.getTime();
    }

    public static Date EarlyHour(Date date, int amount) {
        Calendar now =Calendar.getInstance();
        now.setTime(date);
        now.set(Calendar.HOUR,now.get(Calendar.HOUR)+amount);
        return now.getTime();
    }

    public static Date EarlyMinute(Date date, int amount) {
        Calendar now =Calendar.getInstance();
        now.setTime(date);
        now.set(Calendar.MINUTE,now.get(Calendar.MINUTE)+amount);
        return now.getTime();
    }

    public static void main(String[] args) {
        System.out.println(DateUtil.DetailDate(new Date()));
        System.out.println(DateUtil.DetailDate(DateUtil.EarlyMinute(new Date(), +30)));
        System.out.println(DateUtil.DetailDate(DateUtil.EarlyHour(new Date(), +1)));
        System.out.println(DateUtil.formatDate(new Date()));
        System.out.println(DateUtil.AllDate(new Date()));
        System.out.println(DateUtil.formatDate(DateUtil.addDays(new Date(), -1)));
    }


}
